package com.b2mining.integration.helper;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
//import java.util.Enumeration;
import java.util.Hashtable;
//import java.util.Iterator;
import org.jdom.Attribute;
//import org.jdom.Document;
import org.jdom.Element;
//import org.jdom.input.SAXBuilder;

import com.b2mining.integration.beans.DocumentConfigurationBean;

/**
 * @author db2admin
 * 
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates. To enable and disable the creation of type
 * comments go to Window>Preferences>Java>Code Generation.
 */
public class RequiredElementHelper {

	private static Hashtable configuration = null;

	public RequiredElementHelper() {
		//if (configuration == null) {
			//loadConfiguration();
			// printConfiguration();
		//}
	}

	/*public boolean isRequiredAttribute(String documentName, String attributeName) {
		boolean result = false;
		if (configuration != null && documentName != null
				&& attributeName != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList attributeList = document.getListaAttributos();
				if (attributeList != null
						&& attributeList.contains(attributeName)) {
					result = true;
				}
			}
		}
		return result;

	}*/

	/*public boolean isRequiredElement(String documentName, String elementName) {
		boolean result = false;
		if (configuration != null && documentName != null
				&& elementName != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList elementList = document.getListaElementos();
				if (elementList != null && elementList.contains(elementName)) {
					result = true;
				}
			}
		}
		return result;

	}*/

	public boolean isRequiredGroup(String documentName, String groupName) {
		boolean result = false;
		if (configuration != null && documentName != null && groupName != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList groupList = document.getListaGrupos();
				if (groupList != null && groupList.contains(groupName)) {
					result = true;
				}
			}
		}
		return result;

	}

	public boolean isCorrect(String documentName, String elementName,
			String value) {
		boolean result = true;
		// log.debug("isCorrect "+documentName+" elementName "+elementName+"
		// value "+value);
		if (configuration != null && documentName != null
				&& elementName != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList elementList = document.getListaElementos();
				if (elementList != null && elementList.contains(elementName)) {
					// verificacion del valor de un campo configurado como
					// obligatorio
					// log.debug("IsCorrect hay elementos");
					if (value == null || value.trim().length() == 0) {
						// log.debug("Is correct el campo es nulo o vacio lo
						// elimino");
						result = false;
					}
				}
			}
		}
		// log.debug("Resultado de es correcto "+result);
		return result;
	}

	public boolean isCorrectAttribute(String documentName, String attribute,
			String value) {
		boolean result = true;
		if (configuration != null && documentName != null && attribute != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList attributeList = document.getListaAttributos();
				if (attributeList != null && attributeList.contains(attribute)) {
					// verificacion del valor de un campo configurado como
					// obligatorio

					if (value == null || value.trim().length() == 0) {
						result = false;
					}
				}
			}
		}
		return result;
	}

	public boolean isCorrect(String documentName, String elementName,
			Double value) {
		boolean result = true;
		if (configuration != null && documentName != null
				&& elementName != null) {

			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList elementList = document.getListaElementos();
				if (elementList != null && elementList.contains(elementName)) {
					// verificacion del valor de un campo configurado como
					// obligatorio

					if (value == null) {
						result = false;
					}
				}
			}
		}
		return result;
	}

	public boolean isCorrect(String documentName, String elementName,
			Integer value) {
		boolean result = true;
		if (configuration != null && documentName != null
				&& elementName != null) {

			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList elementList = document.getListaElementos();
				if (elementList != null && elementList.contains(elementName)) {
					// verificacion del valor de un campo configurado como
					// obligatorio

					if (value == null) {
						result = false;
					}
				}
			}
		}
		return result;
	}

	public boolean isCorrect(String documentName, String elementName, Date value) {
		boolean result = true;
		if (configuration != null && documentName != null
				&& elementName != null) {
			DocumentConfigurationBean document = (DocumentConfigurationBean) configuration
					.get(documentName);
			if (document != null) {
				ArrayList elementList = document.getListaElementos();
				if (elementList != null && elementList.contains(elementName)) {
					// verificacion del valor de un campo configurado como
					// obligatorio

					if (value == null) {
						result = false;
					}
				}
			}
		}
		return result;
	}

	/*private void loadConfiguration() {
		// lectura de archivo XML de configuracion
		configuration = new Hashtable();

		//Document doc = null;
		//Element root = null;
		//Element base = null;
		//Element child = null;

		try {
			SAXBuilder builder = new SAXBuilder();
			builder.setValidation(true);
			//doc = builder.build(RequiredElementConstants.FILEPATH);

			Element eDocument = null;
			Element eElementList = null;
			Element eRequiredElement = null;
			Element eGroupList = null;
			Element eRequiredGroup = null;
			Element eAttributeList = null;
			Element eRequiredAttribute = null;

			Iterator iDocument = null;
			Iterator iRequiredElement = null;
			Iterator iRequiredGroup = null;
			Iterator iRequiredAttribute = null;

		//	root = doc.getRootElement();

			String documentName = null;
			String elementName = null;
			String groupName = null;
			String attributeName = null;

			DocumentConfigurationBean documentConfigurationBean = null;
			ArrayList listaElementos = null;
			ArrayList listaGrupos = null;
			ArrayList listaAttributos = null;

//			iDocument = getChildren(root, RequiredElementConstants.DOCUMENT);
//			while (iDocument.hasNext()) {
//				eDocument = (Element) iDocument.next();
//
//				documentConfigurationBean = new DocumentConfigurationBean();
//				listaElementos = new ArrayList();
//				listaGrupos = new ArrayList();
//				listaAttributos = new ArrayList();
//				documentName = checkValue(getChild(
//						RequiredElementConstants.DOCUMENTNAME, eDocument));
//				documentConfigurationBean.setDocumentName(documentName);
//				documentConfigurationBean.setListaElementos(listaElementos);
//				documentConfigurationBean.setListaGrupos(listaGrupos);
//				documentConfigurationBean.setListaAttributos(listaAttributos);
//				configuration.put(documentName, documentConfigurationBean);
//
//				eElementList = getChild(RequiredElementConstants.ELEMENTLIST,
//						eDocument);
//				iRequiredElement = getChildren(eElementList,
//						RequiredElementConstants.REQUIREDELEMENT);
//				while (iRequiredElement != null && iRequiredElement.hasNext()) {
//					eRequiredElement = (Element) iRequiredElement.next();
//					elementName = checkValue(eRequiredElement);
//					if (!listaElementos.contains(elementName)) {
//						listaElementos.add(elementName);
//					}
//				}
//
//				eGroupList = getChild(RequiredElementConstants.GROUPLIST,
//						eDocument);
//				iRequiredGroup = getChildren(eGroupList,
//						RequiredElementConstants.REQUIREDGROUP);
//
//				while (iRequiredGroup != null && iRequiredGroup.hasNext()) {
//					eRequiredGroup = (Element) iRequiredGroup.next();
//					groupName = checkValue(eRequiredGroup);
//					if (!listaGrupos.contains(groupName)) {
//						listaGrupos.add(groupName);
//					}
//				}
//
//				eAttributeList = getChild(
//						RequiredElementConstants.ATTRIBUTELIST, eDocument);
//				iRequiredAttribute = getChildren(eAttributeList,
//						RequiredElementConstants.REQUIREDATTRIBUTE);
//				while (iRequiredAttribute != null
//						&& iRequiredAttribute.hasNext()) {
//					eRequiredAttribute = (Element) iRequiredAttribute.next();
//					attributeName = checkValue(eRequiredAttribute);
//					if (!listaAttributos.contains(attributeName)) {
//						listaAttributos.add(attributeName);
//					}
//				}
//
//			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}*/

	/*public static Element getChild(String name, Element base) {

		if (name == null)
			return null;
		if (base == null)
			return null;
		return base.getChild(name);
	}*/

	/*private Iterator getChildren(Element base, String nameList) {
		if (base == null || nameList == null)
			return null;

		Iterator iterator = null;
		iterator = base.getChildren(nameList).iterator();
		return iterator;
	}*/

	public static String checkValue(Element element) {
		if (element == null) {
			return null;
		}
		if (element.getText() == null) { 
			return null;
		}
		
	
		return element.getTextTrim();
	}

	/*public static String checkValue(Attribute attribute) {
		if (attribute == null)
			return null;
		if (attribute.getValue() == null)
			return null;
		// if (element.getText().equals("")) return null;
		return attribute.getValue().trim();
	}*/

	/*public static String trim(String s) {
		if (s == null)
			return null;
		return s.trim();
	}*/

	/*private void printConfiguration() {
		if (configuration != null) {
			Enumeration enumer = configuration.keys();
			while (enumer.hasMoreElements()) {
				String key = (String) enumer.nextElement();
				System.out.println("\nDOCUMENTO:" + key);
				DocumentConfigurationBean documentConfigurationBean = (DocumentConfigurationBean) configuration
						.get(key);
				ArrayList elementList = documentConfigurationBean
						.getListaElementos();
				for (int j = 0; j < elementList.size(); j++) {
					String element = (String) elementList.get(j);
					System.out.println("\tELEMENTO:" + element);
				}

				ArrayList groupList = documentConfigurationBean
						.getListaGrupos();
				if (groupList.size() > 0)
					System.out.println("\t---------------");
				for (int j = 0; j < groupList.size(); j++) {
					String element = (String) groupList.get(j);
					System.out.println("\tGRUPO:" + element);
				}

				ArrayList attributeList = documentConfigurationBean
						.getListaAttributos();
				if (attributeList.size() > 0)
					System.out.println("\t---------------");
				for (int j = 0; j < attributeList.size(); j++) {
					String element = (String) attributeList.get(j);
					System.out.println("\tATTRIBUTO:" + element);
				}

			}
		}
	}*/

	public String formatDate(Date theDate, SimpleDateFormat formatter) {

		if (theDate == null)
			return "";
		return formatter.format(theDate);

	}

	public String formatInteger(Integer i) {

		if (i == null)
			return "";
		return i.toString();

	}

	/**
	 * Metodo que le da formato a un objeto de la clase Double.
	 * 
	 * @param numero
	 *            numero a formatear
	 * @param formato
	 *            formato a aplicar al numero
	 * @return cadena que contiene al numero formateado
	 */
	public static String formatDouble(Double numero, String formato) {
		if (numero == null)
			return "";

		String numeroFormateado = "";
		DecimalFormatSymbols decimalFormatSymbol = new DecimalFormatSymbols();
		decimalFormatSymbol.setDecimalSeparator('.');
		DecimalFormat decimalFormat = new DecimalFormat(formato,
				decimalFormatSymbol);
		numeroFormateado = decimalFormat.format(numero);
		return numeroFormateado;

	}

	public Date parseDate(String theString, SimpleDateFormat formatter)
			throws ParseException {

		if (theString == null || theString.trim().length() == 0)
			return null;
		return formatter.parse(theString);
	}

	public int parseInt(String theString) throws NumberFormatException {

		if (theString == null || theString.trim().length() == 0)
			return 0;
		return Integer.parseInt(theString);
	}

	public double parseDouble(String theString) throws NumberFormatException {

		if (theString == null || theString.trim().length() == 0)
			return 0;
		return Double.parseDouble(theString);
	}

	public String convertNullToBlank(String theString) {

		if (theString == null)
			return "";
		return theString;
	}

	public boolean isEmpty(String theString) {
		if (theString == null || theString.trim().length() == 0)
			return true;
		return false;
	}

	public static void main(String[] args) {
		//RequiredElementHelper helper = new RequiredElementHelper();
		/*
		 * System.out.println (helper.isCorrect("","","")); System.out.println
		 * (helper.isCorrect("x","","")); System.out.println
		 * (helper.isCorrect("x","x","")); System.out.println
		 * (helper.isCorrect("FACACTCONTMQ","yy","")); System.out.println
		 * (helper.isCorrect("FACACTCONTMQ","RucCliente","xxx"));
		 * System.out.println
		 * (helper.isCorrect("FACACTCONTMQ","RucCliente",""));
		 * System.out.println (helper.isCorrect("FACACTCONTMQ","RucCliente","
		 * "));
		 */
		
//		System.out.println(helper.isRequiredAttribute("POUPLOADMQ", null));
//		System.out.println(helper.isRequiredAttribute("POUPLOADMQ", " "));
//		System.out.println(helper.isRequiredAttribute("POUPLOADMQ",
//				"RucCliente"));
//		System.out.println(helper.isRequiredAttribute("POUPLOADMQ", "Name"));

	}

}
